package cn.ncu.wechat.service.impl;

import cn.ncu.wechat.model.dto.PointRuleDTO;
import cn.ncu.wechat.util.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.ncu.wechat.model.PointsRule;
import cn.ncu.wechat.service.PointsRuleService;
import cn.ncu.wechat.dao.PointsRuleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
* @author 
* @description 针对表【points_rule(积分规则表)】的数据库操作Service实现
* @createDate 2025-01-07 16:16:52
*/
@Service
public class PointsRuleServiceImpl extends ServiceImpl<PointsRuleMapper, PointsRule>
    implements PointsRuleService{
    @Autowired
    private PointsRuleMapper pointsRuleMapper;

    public Result<?> getRule(){

        try {
            PointsRule pointsRule = pointsRuleMapper.selectOne(null);
            PointRuleDTO pointRuleDTO = new PointRuleDTO();
            pointRuleDTO.setConsumePoints(pointsRule.getConsumePointsRate());
            pointRuleDTO.setMinConsumeAmount(pointsRule.getMinConsumeAmount());
            pointRuleDTO.setRegisterPoints(pointsRule.getRegisterPoints());
            return Result.success(pointRuleDTO);
        }catch (Exception e){
            return Result.error(500,"服务器异常:"+e);
        }
    }

    public Result<?> setRule(PointRuleDTO pointRuleDTO){
        try {
            if(pointRuleDTO.getConsumePoints().compareTo(BigDecimal.ZERO)<0
                    ||pointRuleDTO.getMinConsumeAmount().compareTo(BigDecimal.ZERO)<0
                    ||pointRuleDTO.getRegisterPoints()<0){
                return Result.error(400,"参数错误");
            }
            else {
                if(pointsRuleMapper.updatePointRule(pointRuleDTO)>0){
                    return Result.success();
                }
                else return Result.error(500,"服务器错误，操作失败");
            }
        }catch (Exception e){
            return Result.error(500,"服务器异常:"+e);
        }
    }
}




